package ui;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ui.swing.ManageUI;
import util.DateUtil;
import control.IndustryWeight;
import control.TimeStockCompare;
import data.DataBuffer;
import data.LoadData;
import data.ReadAndWriteCareCode;
import data.SaveHistoryData;
import data.TimeStock;
import data.deal.CompanyResult;
import data.deal.SinaIndustryDeal;
import data.net.ResearchHunt;
import dataconnect.ConnectionFactory;
import dataconnect.InsertStockDeal;

public class StockUI {
	
	protected Shell shell;
	protected ShowImage dailyShell;
	private Table table;
	private TabFolder tabFolder;
	private TabItem tbtmNewItem;
	private Composite composite;
	private static TableViewer tableViewer ;
	private Text text;
	private static LinkedHashSet<String> codeSet = ReadAndWriteCareCode.getCodeSet();
	private Connection con = ConnectionFactory.getConnection();
	private StyledText styledText_2;
	private StyledText txtResearchRpt;
	private File file = new File("historydata"+File.separator+"mark.txt");

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoadData ld = new LoadData();
			ld.loadAllData();
			StockUI window = new StockUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setCodeSet(LinkedHashSet<String> codeSete) {
		codeSet = codeSete;
		tableViewer.setInput(getCodeSet());		
	}
	
	public static Set<String> getCodeSet() {
		return codeSet;
	}
	/**
	 * Open the window.
	 * @throws Exception 
	 */
	public void open() throws Exception {
		
		Display display = Display.getDefault();
		createContents();
		
		shell.open();
		shell.layout();
		
		shell.setImage(new Image(display, "image/title.jpg"));
		
//		new SystemTray(shell);

		dailyShell = new ShowImage(display);
		dailyShell.open();
		
		dailyShell.layout();
		dailyShell.setVisible(false);
		
		while (!shell.isDisposed()||dailyShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
			else{
				try{
					tableViewer.refresh();
				}catch(Exception e){
					e.printStackTrace();
				}
				TableItem[] ti = tableViewer.getTable().getSelection();
				if(ti.length>0)
					shell.setText(ti[0].getText(1)+":"+ti[0].getText(2)+" "+ti[0].getText(4));
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		
		shell.setSize(985, 627);
		shell.setText("SWT Application");
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				System.exit(0);
			}
		});

		MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu.setText("\u6587\u4EF6");
		
		Menu menu_1 = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_1);
		
		MenuItem menuItembak = new MenuItem(menu_1, SWT.NONE);
		menuItembak.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							long t = System.currentTimeMillis();
							File file = new File("historydata"+File.separator+"last.sto");
							String fileName = DateUtil.format(DateUtil.getThisDate());
							File file2 = new File("historydata"+File.separator+fileName+".sto");
							try {
								if(file.exists()){
									file.delete();
								}
								if(file2.exists()){
									file2.delete();
								}
								new SaveHistoryData().saveHistoryData();
								System.out.println("save file:"+(System.currentTimeMillis()-t));
								InsertStockDeal.insertStockData(fileName+".sto");
							} catch (IOException ioe) {
								ioe.printStackTrace();
							} catch (SQLException sqle) {
								sqle.printStackTrace();
							}
							long t2 = System.currentTimeMillis();
							System.out.println("备份完成:"+(t2-t));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				
			}
		});
		menuItembak.setText("\u5907\u4EFD");
		
		MenuItem menuItemExit = new MenuItem(menu_1, SWT.NONE);
		menuItemExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.exit(0);
			}
		});
		menuItemExit.setText("\u9000\u51FA");
		
		MenuItem mntmNewSubmenu_1 = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu_1.setText("\u64CD\u4F5C");
		
		Menu menu_2 = new Menu(mntmNewSubmenu_1);
		mntmNewSubmenu_1.setMenu(menu_2);
		
		MenuItem menuItemCare = new MenuItem(menu_2, SWT.NONE);
		menuItemCare.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCodeSet(ReadAndWriteCareCode.getCodeSet());
			}
		});
		menuItemCare.setText("\u5173\u6CE8 F1");
		
		MenuItem menuItemManualSel = new MenuItem(menu_2, SWT.NONE);
		menuItemManualSel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ManualSelectUI manualSelectUI = new ManualSelectUI();
				LinkedHashSet<String> selCode = manualSelectUI.open();
				if(!selCode.isEmpty()){
					setCodeSet(selCode);
				}
			}
		});
		menuItemManualSel.setText("\u81EA\u5B9A\u4E49\u9009\u80A1 F2");
		
		MenuItem menuItemsqlQuery = new MenuItem(menu_2, SWT.NONE);
		menuItemsqlQuery.setText("sql\u9009\u5165");
		menuItemsqlQuery.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SqlInputDialog sid = new SqlInputDialog(shell, 0);
				sid.open();
				if(sid.result!=null&&!sid.result.toString().trim().equals("")){
					String sql = sid.result.toString().trim();
					try {
						Statement stmt = con.createStatement();
						ResultSet rs = stmt.executeQuery(sql);
						LinkedHashSet<String> ts = new LinkedHashSet<String>();
						while(rs.next()){
							ts.add(rs.getString("number"));
						}
						setCodeSet(ts);
						rs.close();
						stmt.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		
		MenuItem menuItem20s = new MenuItem(menu_2, SWT.NONE);
		menuItem20s.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCodeSet(DataBuffer.getSeconds20Code());
			}
		});
		menuItem20s.setText("20\u79D2\u91CF\u589E");
		
		MenuItem menuItem1min = new MenuItem(menu_2, SWT.NONE);
		menuItem1min.setText("1\u5206\u949F\u91CF\u589E");
		menuItem1min.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setCodeSet(DataBuffer.getOneMinCode());
			}
		});
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("\u884C\u4E1A");
		
		final Menu menu_3 = new Menu(menuItem);
		menuItem.setMenu(menu_3);
		
		MenuItem mntmNewItem = new MenuItem(menu, SWT.CASCADE);
		mntmNewItem.setText("\u6570\u636E\u5904\u7406");
		
		Menu menu_4 = new Menu(mntmNewItem);
		mntmNewItem.setMenu(menu_4);
		
		MenuItem menuItemScore = new MenuItem(menu_4, SWT.NONE);
		menuItemScore.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				CompanyResult.main(null);
			}
		});
		menuItemScore.setText("\u66F4\u65B0\u51C0\u5229\u6DA6");
		
		MenuItem mISinaIndustry = new MenuItem(menu_4, SWT.NONE);
		mISinaIndustry.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SinaIndustryDeal.main(null);
			}
		});
		mISinaIndustry.setText("\u66F4\u65B0\u65B0\u6D6A\u884C\u4E1A");
		
		MenuItem menuItem_1 = new MenuItem(menu, SWT.CASCADE);
		menuItem_1.setText("窗口");
		
		Menu menu_5 = new Menu(menuItem_1);
		menuItem_1.setMenu(menu_5);
		
		MenuItem menuSel = new MenuItem(menu_5, SWT.NONE);
		menuSel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							new ManageUI();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		menuSel.setText("显示监控");
		
		String sql = "select distinct industry from stockinfo order by industry";
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				MenuItem menuItemt = new MenuItem(menu_3, SWT.NONE);
				menuItemt.setText(rs.getString(1));
				menuItemt.addSelectionListener(new SelectionAdapter(){
					public void widgetSelected(SelectionEvent e) {
						String industry = ((MenuItem)e.getSource()).getText();
						String sql2 = "select number from stockinfo where industry = '"+industry+"'";
						Statement stmt2 = null;
						ResultSet rs2 = null;
						try {
							stmt2 = con.createStatement();
							rs2 = stmt2.executeQuery(sql2);
							LinkedHashSet<String> ts = new LinkedHashSet<String>();
							while(rs2.next()){
								ts.add(rs2.getString(1));
							}
							setCodeSet(ts);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}finally{
							try {
								rs2.close();
								stmt2.close();
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						}
					}
				});
			}
		}catch (SQLException e1) {
			e1.printStackTrace();
		}finally{
			try {
				rs.close();
				stmt.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		
		tabFolder = new TabFolder(shell, SWT.NONE);
		tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
		composite = new Composite(tabFolder, SWT.NONE);
		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		
		tabFolder.setBounds(0, 0, 925, 522);
		tbtmNewItem.setText("\u5B9E\u65F6\u8868");
		tbtmNewItem.setControl(composite);
		
		table = tableViewer.getTable();
		table.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if(e.keyCode==SWT.CR){
					InputDialog inputDialog = new InputDialog(shell,"标题","提示信息","",null);
					if(inputDialog.open() == InputDialog.OK){
						String value = inputDialog.getValue().toUpperCase();
						String sql = "select * from stockinfo where number like '%"+value+"%' or name like '%"+value+"%' or shortname like '%"+value+"%'";
						System.out.println(sql);
						Statement stmt = null;
						ResultSet rs = null;
						try {
							stmt = con.createStatement();
							rs = stmt.executeQuery(sql);
							SelectDialog sd =new SelectDialog(shell,SWT.NONE,rs);
							sd.open();
							if(sd.result==null){
								return;
							}
							if(!"".equals(sd.result.toString())){
								ReadAndWriteCareCode.addCode(sd.result.toString());
								setCodeSet(ReadAndWriteCareCode.getCodeSet());
							}
							System.out.println(sd.result.toString());
						} catch (SQLException e1) {
							e1.printStackTrace();
						}finally{
							try {
								rs.close();
								stmt.close();
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						}
						System.out.println(value);
					}
				}else if(e.keyCode==SWT.DEL){
					if(table.getSelection().length>0){
						ReadAndWriteCareCode.removeCode(table.getSelection()[0].getText(0));
						setCodeSet(ReadAndWriteCareCode.getCodeSet());
					}
				}else if(e.keyCode==SWT.INSERT){
					ReadAndWriteCareCode.addCode(table.getSelection()[0].getText(0));
				}else if(e.stateMask==SWT.CTRL && e.keyCode==113 ){
					QuerySqlUI querySqlUI = new QuerySqlUI();
					querySqlUI.open();
				}
			}
		});
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(10, 10, 733, 436);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(0);
			}
		});
		tblclmnNewColumn.setWidth(80);
		tblclmnNewColumn.setText("\u4EE3\u7801");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(1);
			}
		});
		tblclmnNewColumn_1.setWidth(86);
		tblclmnNewColumn_1.setText("\u540D\u79F0");
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(2);
			}
		});
		tblclmnNewColumn_2.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_2.setWidth(69);
		tblclmnNewColumn_2.setText("\u5F53\u524D\u4EF7\u683C");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(3);
			}
		});
		tblclmnNewColumn_3.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_3.setWidth(61);
		tblclmnNewColumn_3.setText("\u6DA8\u8DCC");
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(4);
			}
		});
		tblclmnNewColumn_4.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_4.setWidth(66);
		tblclmnNewColumn_4.setText("\u6DA8\u5E45");
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(5);
			}
		});
		tblclmnNewColumn_5.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_5.setWidth(77);
		tblclmnNewColumn_5.setText("\u6210\u4EA4\uFF08\u4E07\uFF09");
		
		TableViewerColumn tableViewerColumn_10 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_10 = tableViewerColumn_10.getColumn();
		tblclmnNewColumn_10.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(6);
			}
		});
		tblclmnNewColumn_10.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_10.setWidth(50);
		tblclmnNewColumn_10.setText("\u589E\u91CF");
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(7);
			}
		});
		tblclmnNewColumn_6.setWidth(65);
		tblclmnNewColumn_6.setText("\u66F4\u65B0\u65F6\u95F4");
		
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_7 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(8);
			}
		});
		tblclmnNewColumn_7.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_7.setWidth(72);
		tblclmnNewColumn_7.setText("\u6700\u9AD8");
		
		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_8 = tableViewerColumn_8.getColumn();
		tblclmnNewColumn_8.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(9);
			}
		});
		tblclmnNewColumn_8.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_8.setWidth(80);
		tblclmnNewColumn_8.setText("\u6700\u4F4E");
		
		TableViewerColumn tableViewerColumn_9 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_9 = tableViewerColumn_9.getColumn();
		tblclmnNewColumn_9.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				final IndustryWeight iw = new IndustryWeight(codeSet);
				List<String> codes = new ArrayList<String>(codeSet);
				Collections.sort(codes,new Comparator<String>(){

					@Override
					public int compare(String o1, String o2) {
						if(o1.equals("sz399001")||o1.equals("sh000001")
								||o2.equals("sz399001")||o2.equals("sh000001")){
							return 0;
						}
						return iw.getIndustryMap().get(LoadData.getTimeStock(o2).getIndustry()).compareTo(iw.getIndustryMap().get(LoadData.getTimeStock(o1).getIndustry()));
					}
					
				});
				codeSet.clear();
				for (String code : codes) {
					codeSet.add(code);
				}
//				sortColumn(10);
			}
		});
		tblclmnNewColumn_9.setWidth(70);
		tblclmnNewColumn_9.setText("\u884C\u4E1A");
		
		TableViewerColumn tableViewerColumn_12 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_11 = tableViewerColumn_12.getColumn();
		tblclmnNewColumn_11.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(11);
			}
		});
		tblclmnNewColumn_11.setWidth(70);
		tblclmnNewColumn_11.setText("新浪行业");
		
		TableViewerColumn tableViewerColumn_11 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_12 = tableViewerColumn_11.getColumn();
		tblclmnNewColumn_12.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(12);
			}
		});
		tblclmnNewColumn_12.setWidth(70);
		tblclmnNewColumn_12.setText("\u8D8B\u52BF");
		
		TableViewerColumn tableViewerColumn_14 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_14 = tableViewerColumn_14.getColumn();
		tblclmnNewColumn_14.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(13);
			}
		});
		tblclmnNewColumn_14.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_14.setWidth(60);
		tblclmnNewColumn_14.setText("\u4EF7\u683C\u53D8\u901F");
		
		TableViewerColumn tableViewerColumn_13 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_13 = tableViewerColumn_13.getColumn();
		tblclmnNewColumn_13.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				sortColumn(14);
			}
		});
		tblclmnNewColumn_13.setWidth(65);
		tblclmnNewColumn_13.setText("\u9009\u5165\u65F6\u95F4");
		
		TableViewerColumn tableViewerColumn_15 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_15 = tableViewerColumn_15.getColumn();
		tblclmnNewColumn_15.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_15.setWidth(69);
		tblclmnNewColumn_15.setText("\u9009\u5165\u4EF7\u683C");
		
		TableViewerColumn tableViewerColumn_16 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_16 = tableViewerColumn_16.getColumn();
		tblclmnNewColumn_16.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(16);
			}
		});
		tblclmnNewColumn_16.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_16.setWidth(61);
		tblclmnNewColumn_16.setText("\u9009\u5165\u76C8\u4E8F");
		
		TableViewerColumn tableViewerColumn_17 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_17 = tableViewerColumn_17.getColumn();
		tblclmnNewColumn_17.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(17);
			}
		});
		tblclmnNewColumn_17.setWidth(65);
		tblclmnNewColumn_17.setText("\u91CF\u589E\u65F6\u95F4");
		
		TableViewerColumn tableViewerColumn_18 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_18 = tableViewerColumn_18.getColumn();
		tblclmnNewColumn_18.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortColumn(18);
			}
		});
		tblclmnNewColumn_18.setAlignment(SWT.RIGHT);
		tblclmnNewColumn_18.setWidth(50);
		tblclmnNewColumn_18.setText("\u500D\u6570");
		
		TableViewerColumn tableViewerColumn_20 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_20 = tableViewerColumn_20.getColumn();
		tblclmnNewColumn_20.setWidth(100);
		tblclmnNewColumn_20.setText("上攻次数");
		
		TableViewerColumn tableViewerColumn_19 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_19 = tableViewerColumn_19.getColumn();
		tblclmnNewColumn_19.setWidth(100);
		tblclmnNewColumn_19.setText("备注");
		
		tableViewer.setLabelProvider(new TableLabelProvider());
		tableViewer.setContentProvider(new ContentProvider());
		tableViewer.setInput(getCodeSet());
		
		TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_1.setText("\u6D4F\u89C8\u5668");
		
		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem_1.setControl(composite_1);
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setBounds(0, 0, 32, 21);
		lblNewLabel.setText("\u7F51\u5740\uFF1A");
		
		text = new Text(composite_1, SWT.BORDER);
		text.setBounds(39, 0, 582, 21);
		
		Button button = new Button(composite_1, SWT.NONE);
		button.setBounds(624, 0, 35, 21);
		button.setText("\u8F6C\u5230");
		
		final Browser browser = new Browser(composite_1, SWT.NONE);
		browser.setBounds(10, 28, 806, 480);
		
		final Combo combo = new Combo(composite_1, SWT.NONE);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(combo.getSelectionIndex()==0){
					String url = "http://www.newone.com.cn/";
					browser.setUrl(url);
					text.setText(url);
				}else if(combo.getSelectionIndex()==1){
					String url = "http://stock.stockstar.com/report/data_all.htm";
					browser.setUrl(url);
					text.setText(url);
				}else if(combo.getSelectionIndex()==2){
					String url = "http://finance.sina.com.cn/stock/";
					browser.setUrl(url);
					text.setText(url);
				}else if(combo.getSelectionIndex()==3){
					String url = "http://stockhtm.finance.qq.com/report/others/jggpc.html";
					browser.setUrl(url);
					text.setText(url);
				}
			}
		});
		combo.setItems(new String[] {"\u62DB\u5546\u7814\u7A76", "\u8BC1\u5238\u4E4B\u661F", "\u65B0\u6D6A\u80A1\u7968", "\u817E\u8BAF\u80A1\u7968"});
		combo.setBounds(667, 0, 88, 21);
		
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				browser.setUrl(text.getText());
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				Table t = (Table)e.getSource();
				TableItem[] ti = t.getSelection();
				if(ti.length>0){
					String code =ti[0].getText(0);
					String url = "http://finance.sina.com.cn/realstock/company/"+code+"/nc.shtml";
					browser.setUrl(url);
					text.setText(url);
				}
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				if(e.button==3){
					Table t = (Table)e.getSource();
					TableItem[] ti = t.getSelection();
					if(ti.length>0){
						String code =ti[0].getText(0);
						dailyShell.setBounds(e.x+15, e.y+80, 550, 310);
						dailyShell.draw("http://image.sinajs.cn/newchart/daily/n/"+code+".gif");
						dailyShell.setActive();
					}
				}else{
					dailyShell.setVisible(false);
				}
			}
			
		});
		
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				String url = text.getText();
				if(e.stateMask == SWT.CTRL&&e.keyCode==SWT.CR){
					url = "http://www." + url + ".com";
					browser.setUrl(url);
					text.setText(url);
				}else if(e.keyCode==13){
					if(url.indexOf("http")<0){
						url="http://"+url;
					}
					browser.setUrl(url);
					text.setText(url);
				}
			}
		});
		
		shell.setMaximized(true);
		
		TabItem tabItem_3 = new TabItem(tabFolder, SWT.NONE);
		tabItem_3.setText("\u64CD\u4F5C\u5907\u5FD8");
		
		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tabItem_3.setControl(composite_2);
		
		TextViewer textViewer_2 = new TextViewer(composite_2, SWT.BORDER);
		styledText_2 = textViewer_2.getTextWidget();
		styledText_2.setBounds(10, 10, 868, 424);
		
		TabItem tabItem_4 = new TabItem(tabFolder, SWT.NONE);
		tabItem_4.setText("\u7814\u62A5");
		
		Composite composite_3 = new Composite(tabFolder, SWT.NONE);
		tabItem_4.setControl(composite_3);
		
		txtResearchRpt = new StyledText(composite_3, SWT.WRAP|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
		txtResearchRpt.setLocation(10, 30);
		txtResearchRpt.setSize(436, 265);
		txtResearchRpt.setEditable(false);
		txtResearchRpt.setWordWrap(true);
		final ResearchHunt rsh = new ResearchHunt(txtResearchRpt);
		Button btnQueryResearch = new Button(composite_3, SWT.NONE);
		btnQueryResearch.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				rsh.refresh(1);
			}
		});
		btnQueryResearch.setBounds(0, 0, 80, 27);
		btnQueryResearch.setText("\u62DB\u5546\u7814\u62A5");
		
		Button btnCurMessage = new Button(composite_3, SWT.NONE);
		btnCurMessage.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				rsh.refresh(2);
			}
		});
		btnCurMessage.setBounds(85, 0, 80, 27);
		btnCurMessage.setText("\u4ECA\u65E5\u89C6\u70B9");
		
		try {
			if(file.exists()){
				BufferedReader br = new BufferedReader(new FileReader(file));
				StringBuffer strB = new StringBuffer();
				String str ;
				while((str = br.readLine())!=null){
					strB.append(str);
					strB.append("\n");
				}
				styledText_2.setText(strB.toString());
			}
			
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		styledText_2.addKeyListener(new KeyListener() {
			@Override
			public void keyReleased(KeyEvent e) {
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				 if (e.stateMask == SWT.CTRL && e.keyCode ==115) {
					 try {
						FileWriter wr = new FileWriter(file);
						BufferedWriter bw = new BufferedWriter(wr);
						bw.write(styledText_2.getText());
						bw.flush();
						bw.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				 }
			}
		});
		
		shell.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				tabFolder.setBounds(3, 3, shell.getBounds().width-40, shell.getBounds().height-90);
				table.setBounds(3, 3, tabFolder.getBounds().width-50, tabFolder.getBounds().height-60);
				browser.setBounds(10, 36, tabFolder.getBounds().width-20,tabFolder.getBounds().height-80);
				styledText_2.setBounds(3, 3, tabFolder.getBounds().width-50, tabFolder.getBounds().height-75);
				txtResearchRpt.setBounds(3, 33, tabFolder.getBounds().width-50, tabFolder.getBounds().height-75);
			}
		});
		
		table.addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode==SWT.F1){
					setCodeSet(ReadAndWriteCareCode.getCodeSet());
				}else if(e.keyCode==SWT.F2){
//					setCodeSet(DataBuffer.getOneMinCode());
					ManualSelectUI manualSelectUI = new ManualSelectUI();
					LinkedHashSet<String> selCode = manualSelectUI.open();
					if(!selCode.isEmpty()){
						setCodeSet(selCode);
					}
				}else if(e.keyCode==SWT.F3){
					setCodeSet(DataBuffer.getThreeMinCode());
				}else if(e.keyCode==SWT.F4){
//					setCodeSet(DataBuffer.getSelectCode());
				}
				else if(e.keyCode==SWT.F8){
					setCodeSet(DataBuffer.getIndustryOrder());
				}else if(e.keyCode==SWT.F7){
					setCodeSet(DataBuffer.getSinaIndustryOrder());
				}else if(e.keyCode==SWT.F5){
					setCodeSet(DataBuffer.getFinalSel());
				}else if(e.keyCode==SWT.F12){
					Table t = (Table)e.getSource();
					TableItem[] ti = t.getSelection();
					if(ti.length>0){
						String code =ti[0].getText(0);
						AnalyseInfoUI aui = new AnalyseInfoUI(shell, SWT.NONE,code);
						aui.open();
					}else{
						AnalyseInfoUI aui = new AnalyseInfoUI(shell, SWT.NONE,"sz399001");
						aui.open();
					}
				}
			}
		});
	}
	
	private void sortColumn(int col){
		List<TimeStock> tsList = new ArrayList<TimeStock>();
		for(String code :codeSet){
			TimeStock ts=LoadData.getTimeStock(code);
			if(ts!=null){
				tsList.add(ts);
			}
		}
		
		Collections.sort(tsList,new TimeStockCompare(col));
		codeSet.clear();
		for (TimeStock timeStock : tsList) {
			codeSet.add(timeStock.getCode());
		}
	}
}
